<?php
// 应用公共文件

if (!function_exists('getSiteUrl')) {
    /**
     * 获取当前站点的访问路径根目录
     * @return [type] [description]
     */
    function getSiteUrl()
    {
        $uri = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : ($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
        return 'http://' . $_SERVER['HTTP_HOST'] . substr($uri, 0, strrpos($uri, '/') + 1);
    }
}

if (!function_exists('getDomain')) {
    /**
     * 获取当前站点的访问路径根目录
     * @return [type] [description]
     */
    function getDomain()
    {
        $uri = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : ($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
        $port=$_SERVER['SERVER_PORT'];

        return 'http://' . $_SERVER['HTTP_HOST'] . substr($uri, 0, strrpos($uri, '/') + 1);
    }
}

if (!function_exists('getVirRootDir')) {
    /**
     * 获取虚拟目录路径
     * @return bool|string
     */
    function getVirRootDir()
    {
        $url = $_SERVER['SCRIPT_NAME'];
        $url = substr($url, 0, strripos($url, "/") );
        return $url;
    }
}

if (!function_exists('returnFormat')) {
    /**
     * 格式化输出返回值
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return mixed
     */
    function returnFormat($code=0,$msg="",$data=[]){
        $res['code']=$code;
        $res['data']=$data;
        $res['msg']=$msg;
        return $res;
    }
}

if (!function_exists('getNow')) {
    /**
     * 获取当时时间
     * @param string $fmt 格式化
     * @return false|string
     */
    function getNow($fmt = "Y-m-d H:i:s")
    {
        return date($fmt);
    }
}

if (!function_exists('getUrl')) {
    /**
     * 获取当前站点的访问路径根目录
     * @return [type] [description]
     */
    function getUrl()
    {
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
        $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
        $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
        $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
        return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }
}

if (!function_exists('getallheaders'))
{
    function getallheaders()
    {
        $headers = array ();
        foreach ($_SERVER as $name => $value)
        {
            if (substr($name, 0, 5) == 'HTTP_')
            {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}


function generateReceipt($params){
    $editor = \Grafika\Grafika::createEditor();
//    $font = \Grafika\Grafika::fontsDir() . DIRECTORY_SEPARATOR . 'simhei.ttf';
    $font = public_path('public/static').'simhei.ttf';
//    var_dump($font);exit;
    \think\facade\Log::record("字体文件路径：","debug");
    \think\facade\Log::record($font,"debug");
//    var_dump(public_path('public\\static').'receipt.png');
    $editor->open($image , public_path('public/static').'receipt.png');
    //设置缴费日期
    $editor->text($image ,$params['date'],25,300,127, new \Grafika\Color("#000000"), $font);

    //设置收据编码
    $editor->text($image ,$params['number'],20,1500,128, new \Grafika\Color("#000000"), $font);

    //设置姓名
    $editor->text($image ,$params['studentName'],25,250,217, new \Grafika\Color("#000000"), $font);

    //设置院校
    $editor->text($image ,$params['school'],25,702,217, new \Grafika\Color("#000000"), $font);

    //设置层次
    $editor->text($image ,$params['level'],25,1370,217, new \Grafika\Color("#000000"), $font);

    //设置专业
    $editor->text($image ,$params['specialty'],25,246,295, new \Grafika\Color("#000000"), $font);

    //设置年级
    $editor->text($image ,$params['grade'],25,1370,295, new \Grafika\Color("#000000"), $font);

    //设置订单名称
    $editor->text($image ,$params['itemName'],20,246,485, new \Grafika\Color("#000000"), $font);

    //设置订单金额
    $editor->text($image ,number_format($params['amount'], 2),25,1015,485, new \Grafika\Color("#000000"), $font);

    //设置订单收款方式
    $editor->text($image ,$params['payWay'],25,1484,485, new \Grafika\Color("#000000"), $font);

    //设置订单合计
    $editor->text($image ,number_format($params['totalAmount'], 2),25,1015,560, new \Grafika\Color("#000000"), $font);

    //设置订单实收金额
    $editor->text($image ,number2chinese($params['receiptAmount']),25,380,644, new \Grafika\Color("#000000"), $font);

    //设置订单收款单位
    $editor->text($image ,'韶关市志诚教育科技有限公司',20,1370,644, new \Grafika\Color("#000000"), $font);

    //设置服务热线
    $editor->text($image ,'13827917187',25,260,710, new \Grafika\Color("#000000"), $font);
    $fileName = date('YmdHis').'.png';

    $path = public_path('public/static/file').$fileName;
//    var_dump($path);
    //保存图片并添加收据
    $editor->save($image,$path);

    return 'http://'.$_SERVER['HTTP_HOST']. '/static/file/'.$fileName;

//    return 'http://'.$_SERVER['HTTP_HOST']. '/zhichengedu/api/public/static/file/'.$fileName;
}

function number2chinese($num){
    $arr = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖');
    $cny = array('','拾','佰','仟','','萬','亿','兆');
    //小数部分
    $retval = '';
    if(strpos($num,'.')!==false)
    {
        list($num,$dec) = explode('.',$num);
        $retval  .= $arr[$dec[0]].'角'.$arr[$dec[1]].'分';
    }
    //整数部分
    $str = $num!='0'?strrev($num):'';
    $out =array();
    for($i=0;$i<strlen($str);$i++)
    {
        $out[$i] = $arr[$str[$i]];
        $out[$i] .= $str[$i]!= '0' ? $cny[$i%4] : '';
        if($i>1 && $str[$i]+$str[$i-1]==0)
        {
            $out[$i]='';
        }
        if($i%4==0)
        {
            $out[$i] .= $cny[4+floor($i/4)];
        }
        //echo $out[$i].'<br>';
    }
    $retval = implode('',array_reverse($out)).'元' . $retval;
    return $retval;
}

//判断几秒、几分钟、几天前的函数 $the_time需要计算的时间
function time_tran($the_time)
{
    $now_time = date("Y-m-d H:i:s");
    $now_time = strtotime($now_time);
    $show_time = strtotime($the_time);
    if(!$the_time){
        return '0秒前';
    }
    $dur = $now_time - $show_time;
    if ($dur < 0) {
        return $the_time;
    } else {
        if ($dur < 60) {
            return $dur . '秒前';
        } else {
            if ($dur < 3600) {
                return floor($dur / 60) . '分钟前';
            } else {
                if ($dur < 86400) {
                    return floor($dur / 3600) . '小时前';
                } else {
                    if ($dur >= 86400) {//3天内
                        return floor($dur / 86400) . '天前';
                    } else {
                        return $the_time;
                    }
                }
            }
        }
    }
}

function numeral($num){
    $china=array('零','一','二','三','四','五','六','日');
    $arr=str_split($num);
    for($i=0;$i<count($arr);$i++){
        return $china[$arr[$i]];
    }
}


